package view; 

import java.util.HashMap;
import java.util.Map;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;


/**
 * <pre>
 * view 
 * FragmentPresenter.java
 *
 * 설명 :
 * </pre>
 * 
 * @since : 2013. 8. 23.
 * @author : kwma
 * @version : v1.0
 */
public class FragmentPresenter {

	private FragmentManager fragManager;
	private FragmentTransaction ft;
	
	private static FragmentPresenter mInstance;
	private FragmentPresenter() { initFragments(); }
	
	static { mInstance = new FragmentPresenter(); }
		
	
	public void initFragments() {
		
		addFragment(null, PatterListFragment.class.getName(), new PatterListFragment());
		addFragment(null, SettingFragment.class.getName(), new SettingFragment());
		
		
	}
	
	
	
	public static FragmentPresenter getInstance() {
		if(mInstance == null) {
			return new FragmentPresenter();
		}
		return mInstance;
	}
	

	public void addFragment(Bundle bundle, String key,Fragment fragment) {
		
		ft = fragManager.beginTransaction();
		ft.add(fragment, key);
		ft.commitAllowingStateLoss();
	}
	
	public void removeFragment(String tag) {

		ft = fragManager.beginTransaction();
		ft.remove(fragManager.findFragmentByTag(tag));
		ft.commitAllowingStateLoss();
	}
	
	public void replaceFragment(int containerId, Fragment fragment) {
	
		ft = fragManager.beginTransaction();
		ft.replace(containerId, fragment);
		ft.commitAllowingStateLoss();
	}
	
	public void replaceFragmentByTag(int containerId, Fragment fragment, String tag) {
		
		ft = fragManager.beginTransaction();
		ft.replace(containerId, fragment, tag);
		ft.commitAllowingStateLoss();
	}
		
	public Fragment getFragment(String tag) {
		return fragManager.findFragmentByTag(tag);
	}
	
	public void setFragmentManager(FragmentManager fragManager) {
		this.fragManager = fragManager;
	}
	
	

}
